package com.hsgene.message.controller;

import com.hsgene.common.util.result.ApiResult;
import com.hsgene.message.constant.SMSErrorCode;
import com.hsgene.message.domain.SmsListInfo;
import com.hsgene.message.dto.SmsAddInfoDto;
import com.hsgene.message.dto.SmsManagerConditionDto;
import com.hsgene.message.service.SmsManagerService;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 消息中心Controller
 *
 * @author wxf
 * @date 2018/11/14 15:20
 **/
@RestController
@RequestMapping("/v1.0/genetic/testing/sms")
public class SmsManagerController {
    /**
     * Log
     */
    private static Logger LOGGER = LogManager.getLogger(SmsManagerController.class);

    @Resource
    private SmsManagerService smsManagerService;

    /**
     * 获取消息列表
     *
     * @param smsDto 参数条件
     * @return
     */
    @GetMapping
    public ApiResult<SmsListInfo> getSmsList(SmsManagerConditionDto smsDto) {
        try {
            if (smsDto == null || smsDto.getPage() == null || smsDto.getPage() == 0 || smsDto.getSize() == null) {
                LOGGER.warn("page and size is not null");
                return ApiResult.fail(SMSErrorCode.PAGE_SIZE_IS_NOT_NULL);
            }
            if (smsDto.getPage() < 0) {
                return ApiResult.fail(SMSErrorCode.PAGE_IS_NOT_LESS_THAN_ZERO);
            }
            SmsListInfo infos = smsManagerService.getSmsList(smsDto);
            return new ApiResult(200, "success", "查询成功", infos);
        } catch (Exception e) {
            LOGGER.error("query sms list is error...", e);
            return ApiResult.fail(SMSErrorCode.SMS_LIST_IS_ERROR);
        }
    }

    /**
     * 处理更新
     *
     * @param smsId smsId
     * @return
     */
    @PutMapping
    public ApiResult updateDealInfo(String smsId) {
        try {
            if (StringUtils.isEmpty(smsId)) {
                return ApiResult.fail(SMSErrorCode.SMS_ID_IS_NOT_NULL);
            }
            //查看是否已处理
            Integer flag = smsManagerService.checkDealById(smsId);
            if (flag != null) {
                return ApiResult.fail(SMSErrorCode.SMS_ALREADY_DEAL);
            }
            Boolean result = smsManagerService.updateDealInfo(smsId);
            if (result) {
                return new ApiResult(200, "success", "处理更新成功", result);
            }
            return ApiResult.fail(SMSErrorCode.SMS_DEAL_UPDATE_IS_ERROR);
        } catch (Exception e) {
            LOGGER.error("query sms list is error...", e);
            return ApiResult.fail(SMSErrorCode.SMS_DEAL_UPDATE_IS_ERROR);
        }
    }

    /**
     * 消息增加
     *
     * @param addInfoDto 信息
     * @return
     */
    @PostMapping
    public ApiResult addSms(@RequestBody SmsAddInfoDto addInfoDto) {
        try {
            if (addInfoDto == null) {
                return ApiResult.fail(SMSErrorCode.SMS_IS_NOT_NULL);
            }
            Boolean result = smsManagerService.addSms(addInfoDto, new Date());
            if (result) {
                return new ApiResult(200, "success", "添加消息成功", result);
            }
            return ApiResult.fail(SMSErrorCode.SMS_ADD_IS_ERROR);
        } catch (Exception e) {
            LOGGER.error("add sms is error...", e);
            return ApiResult.fail(SMSErrorCode.SMS_ADD_IS_ERROR);
        }
    }
}
